﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QLKDS.DTO;

namespace QLKDS.DAL
{
    public class LevelManager
    {
        public LevelInfo GetLevelById(int id)
        {
            using (var db = new QLKDSDataContext())
            {
                var queryLevel = from d in db.Levels
                                    where d.Id.Equals(id)
                                    select new LevelInfo { Id = d.Id,  Name = d.Name,Description=d.Description};

                return queryLevel.First();
            }
        
        }


        public IList<LevelInfo> ListLevel()
        {
            using (var db = new QLKDSDataContext())
            {
                var queryLevel = from d in db.Levels
                                   select new LevelInfo { Id = d.Id,Name = d.Name,Description=d.Description };

                return queryLevel.ToList();
            }
        }
    

        public void CreateLevel(Level Level)
        {
            using (var db = new QLKDSDataContext())
            {
                db.Levels.InsertOnSubmit(Level);

                db.SubmitChanges();
               
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="Level"></param>
        public void UpdateLevel(Level Level)
        {
            using (var db = new QLKDSDataContext())
            {
                var _Level = db.Levels.Single(d => d.Id.Equals(Level.Id));

                _Level.Name = Level.Name;
                _Level.Description = Level.Description;
                db.SubmitChanges();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        public void DeleteLevel(int id)
        {
            using (var db = new QLKDSDataContext())
            {
                var _Level = db.Levels.Single(d => d.Id.Equals(id));

                db.Levels.DeleteOnSubmit(_Level);
                db.SubmitChanges();
            }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public bool HasNameExisted(string name)
        {
            using (var db = new QLKDSDataContext())
            {
                var list = (from d in db.Levels
                            where d.Name.Equals(name)
                            select d.Id).ToList();

                return list.Count > 0;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="code"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool HasNameDuplicated(string name, long id)
        {
            using (var db = new QLKDSDataContext())
            {
                var list = (from d in db.Levels
                            where d.Name.Equals(name)
                            where !d.Id.Equals(id)
                            select d.Id).ToList();

                return list.Count > 0;
            }
        }

     
    }
}
